package com.zcpt.controller;

import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zcpt.pojo.Project;
import com.zcpt.service.ProjectService;

@Controller
@RequestMapping("/project")
public class ProjectController {
	@Resource
	private ProjectService projectService;

	// 返回项目id的信息
	@RequestMapping(value = "/{id}/proj", method = RequestMethod.GET)
	public String getProjectById(@PathVariable("id") Integer projectId, Model model) {
		if (projectId == null) {
			return "redirect:/project/error";
		}
		Project project = projectService.getProjectById(projectId);
		if (project == null) {
			return "redirect:/project/error";
		}
		model.addAttribute("proj", project);
		return "proj";
	}

	// 返回该项目id的信息和参与者信息
	@RequestMapping(value = "/{id}/proj_par", method = RequestMethod.GET)
	public String getProjectByIdWithPartner(@PathVariable("id") Integer projectId, Model model) {
		if (projectId == null) {
			return "redirect:/project/error";
		}
		Project project = projectService.getProjectByIdWithPartner(projectId);
		if (project == null) {
			return "redirect:/project/error";
		}
		model.addAttribute("proj_par", project);
		return "proj_par";
	}

	// 返回所有项目信息
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String getAllProject(Model model) {
		List<Project> proList = projectService.getAllProject();
		model.addAttribute("list", proList);
		return "proj_list";
	}

	// 添加项目信息
	@RequestMapping(value = "/add", method = RequestMethod.POST)
	public String addProject(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		try {
			String projectName = req.getParameter("projName");
			String projectDesc = req.getParameter("projDesc");
			String aimSum = req.getParameter("aimSum");
			String createTime = req.getParameter("createTime");
			String closeTime = req.getParameter("closeTime");
			Project pro = new Project();
			pro.setProjectName(projectName);
			pro.setProjectDesc(projectDesc);
			pro.setAimSum(Double.parseDouble(aimSum));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			pro.setCreateTime(sdf.parse(createTime));
			pro.setCloseTime(sdf.parse(closeTime));
			if (projectService.addProject(pro)) {
				return "add_success";
			} else {
				return "add_failure";
			}
		} catch (NumberFormatException e) {
			return "redirect:/project/error";
		} catch (ParseException e) {
			return "redirect:/project/error";
		}
	}

	// 修改项目信息
	@RequestMapping(value = "/alter", method = RequestMethod.POST)
	public String alterProject(HttpServletRequest req) {
		try {
			req.setCharacterEncoding("utf-8");
		} catch (UnsupportedEncodingException e1) {
			// TODO 自动生成的 catch 块
			e1.printStackTrace();
		}
		int projectId = Integer.parseInt(req.getParameter("projId"));
		Project project = projectService.getProjectById(projectId);
		if (project == null) {
			return "redirect:/project/error";
		}
		try {
			String projectName = req.getParameter("projName");
			String projectDesc = req.getParameter("projDesc");
			String aimSum = req.getParameter("aimSum");
			String createTime = req.getParameter("createTime");
			String closeTime = req.getParameter("closeTime");
			Project pro = new Project();
			pro.setProjectId(projectId);
			pro.setProjectName(projectName);
			pro.setProjectDesc(projectDesc);
			pro.setAimSum(Double.parseDouble(aimSum));
			SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
			pro.setCreateTime(sdf.parse(createTime));
			pro.setCloseTime(sdf.parse(closeTime));
			if (projectService.alterProject(pro)) {
				return "alter_success";
			} else {
				return "alter_failure";
			}
		} catch (NumberFormatException e) {
			return "redirect:/project/error";
		} catch (ParseException e) {
			return "redirect:/project/error";
		}

	}

	@RequestMapping(value = "/error")
	public String error() {
		return "error";
	}
}
